﻿using System;
using System.Collections.Generic;
using System.Web;

    public class Order : BaseBAL
    {
        public int OrderId { get; set; }
        public int UserId { get; set; }
        public string OrderCustomerName { get; set; }
        public string OrderCustomerNote { get; set; }
        public string OrderCustomerPhone { get; set; }
        public string OrderCustomerAddress { get; set; }
        public string OrderCustomerEmail { get; set; }

        public DateTime CreatedDate { get; set; }

        public string ManagerNote { get; set; }
        public int UserIdControlOrder { get; set; }
        public int IsFinish { get; set; }
        public int IsCancel { get; set; }
        public int IsViewed { get; set; }
        public int IsContactToCustomer { get; set; }
        public int IsShipping { get; set; }

        public string UserName { get; set; }
        public IList<OrderDetail> OrderDetails { get; set; }

        private COrderFacade facade = new COrderFacade();
        public void RemoveOrderDetail(int pi)
        {
            if (OrderDetails != null)
            {
                foreach (OrderDetail item in OrderDetails)
                {
                    if (item.ProductId == pi)
                    {
                        OrderDetails.Remove(item);
                        break;
                    }
                }
            }
        }
        public void UpdateOrderDetail(int pi, int quantity)
        {
            if (OrderDetails != null)
            {
                foreach (OrderDetail item in OrderDetails)
                {
                    if (item.ProductId == pi)
                    {
                        item.Quantity = quantity;
                        break;
                    }
                }
            }
        }
        public void AddOrderDetail(OrderDetail detail)
        {
            if (OrderDetails != null)
            {
                bool isExists = false;
                foreach (OrderDetail item in OrderDetails)
                {
                    if (item.ProductId == detail.ProductId)
                    {
                        isExists = true;
                        item.Quantity += detail.Quantity;
                        break;
                    }
                }
                if (!isExists)
                {
                    OrderDetails.Add(detail);
                }
            }
        }
        public int TotalQuantiy()
        {
            int ret = 0;
            foreach (OrderDetail item in OrderDetails)
            {
                ret += item.Quantity;
            }
            return ret;
        }
        public int TotalPrice()
        {
            int price = 0;
            foreach (OrderDetail item in OrderDetails)
            {
                price += item.TotalPrice();
            }
            return price;
        }

        public IList<Order> List(int pageIndex, int pageMax, int userid, int isView, int isFinish, int isCancel, DateTime fromDate, DateTime toDate)
        {
            return facade.List(pageIndex, pageMax, userid, isView, isFinish, isCancel, fromDate, toDate);
        }
        public string Insert(Order order)
        {
            return facade.Insert(order);
        }
        public Order Select(int orderid)
        {
            return facade.Select(orderid);
        }
        public IList<OrderDetail> SelectDetail(int orderid)
        {
            return facade.SelectDetail(orderid);
        }
        public string Cancel(int oi)
        {
            return facade.Cancel(oi);
        }

        public string Finish(int oi)
        {
            return facade.Finish(oi);
        }
    }